<?php
namespace App\Admin\Controllers\Basics;

use App\Jobs\Basics\TransportFeeRelate;
use App\Jobs\Basics\TransportRelate;
use App\Jobs\Basics\TransportSeaDayRelate;
use App\Models\Basics\SysIArea;
use App\Models\Basics\Transport;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Jobs\Basics\TransportDayRelate;
class TransportController extends AdminController{

    protected $title = '物流时效设置';

    protected function grid()
    {
        $grid = new Grid(new Transport());

        $grid->column('area.area','站点');
        $grid->column('air_fee','空运费用')->help('利润表核算成本');
        $grid->column('sea_fee','海运费用')->help('利润表核算成本');
        $grid->column('air_day','空运天数')->help('备货提货核算系数');
        $grid->column('sea_day','海运天数')->help('备货提货核算系数');
        $grid->column('updated_at','更新时间');

        $grid->disableFilter();
        $grid->disableExport();
        if(Admin::user()->can('transport.edit')){
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableDelete();
            });
        }else{
            $grid->disableActions();
            $grid->disableCreateButton();
        }

        $grid->disableRowSelector();

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new Transport());
        $form->hidden('id');
        $form->select('area_id','站点')
            ->options(SysIArea::options())
            ->creationRules(['required', "unique:b_transport"])
            ->updateRules(['required', "unique:b_transport,area_id,{{id}}"])
            ->setWidth(4);
        $form->decimal('air_fee','空运费用')->options(['digits'=>4])->attribute(['maxlength'=>8])->setWidth(4)->width('100%')->help('利润表核算成本');
        $form->decimal('sea_fee','海运费用')->options(['digits'=>4])->attribute(['maxlength'=>8])->setWidth(4)->width('100%')->help('利润表核算成本');
        $form->text('air_day','空运天数')->setWidth(4)->help('备货提货核算系数');
        $form->text('sea_day','海运天数')->setWidth(4)->help('备货提货核算系数');
        $form->tools(function (Form\Tools $tools){
            $tools->disableView();
            $tools->disableDelete();
        });
        $form->footer(function (Form\Footer $footer){
           $footer->disableReset();
        });
        $form->saving(function (Form $form) {
            // 判断是否是新增操作
            if ($form->isCreating()) {
                //新增物流运输
                TransportRelate::dispatch($form->area_id,$form->air_fee,$form->sea_fee,$form->air_day,$form->sea_day);
            }else{
                if(!is_null($form->air_fee) && $form->model()->air_fee != $form->air_fee
                    || !is_null($form->sea_fee) && $form->model()->sea_fee != $form->sea_fee){
                    //物流费用
                    TransportFeeRelate::dispatch($form->area_id,$form->air_fee,$form->sea_fee);
                }
                if(!is_null($form->air_day) && $form->model()->air_day != $form->air_day
                    || !is_null($form->sea_day) && $form->model()->sea_day != $form->sea_day){
                    TransportDayRelate::dispatch($form->area_id,$form->air_day,$form->sea_day);
                    if(!is_null($form->sea_day) && $form->model()->sea_day != $form->sea_day){
                        //物流天数
                        TransportSeaDayRelate::dispatch($form->area_id,$form->sea_day);
                    }
                }
            }
        });
        return $form;
    }
}
